/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.tabel.models;

import java.util.Collection;
import org.opu.db_vdumper.ui.component.tabel.views.TableView;
import java.util.HashSet;
import java.util.Set;
import org.opu.db_vdumper.beans.ColumnBean;
import org.opu.db_vdumper.beans.TableBean;
import org.opu.db_vdumper.util.StringUtil;

/**
 *
 * @author Potochnak Yana
 */
public class TableModel extends Model<SelectedComponent>{
    
    private TableBean bean;
    private Set<ArrowModel> arrows;
    private Set<ColumnModel> columns;

    /**
     *  Create new instance
     * @param name name of tabl
     * @param schema schema where table is, can be null
     */
    public TableModel(TableBean bean) {
        this.bean = bean;
        this.arrows = new HashSet<ArrowModel>();
        this.columns = new HashSet<ColumnModel>();

        for (ColumnBean c : this.bean.getColumns()) {
            this.columns.add(new ColumnModel(this, c));
        }
    }

    /**
     *  Return name of tabl
     * @return name of tabl
     */
    public String getName() {
        return String.valueOf(bean.getName());
    }

    /**
     *  Return schema where table
     * @return schema where table
     */
    public String getSchema() {
        return bean.getSchema();
    }

    /**
     *  Create string array from all attributes
     * @return string array from all attributes
     */
    public ColumnBean[] attributesArray() {
        return bean.getColumns().toArray(new ColumnBean[]{});
    }

    /**
     *  Get attribute at the specified position
     * @param index the index of the attribute to be geted
     * @return the attribute at the specified position
     */
    public String getAttribute(int index) {
        return bean.get(index).getName();
    }

    /**
     *  Get attribute at the specified position
     * @param index the index of the attribute to be geted
     * @return the attribute at the specified position
     */
    public ColumnBean attribute(int index) {
        return bean.get(index);
    }
    
    /**
     *  Get attribute with the specified name
     * @param index the index of the attribute to be geted
     * @return the attribute with the specified name
     */
    public ColumnBean attribute(String name) {
        for (ColumnBean c : bean.getColumns()) {
            if (StringUtil.equals(name, c.getName())){
                return c;
            }
        }
        
        return null;
    }

    /**
     *  Return count of attributes
     * @return count of attributes
     */
    public int attributesCount() {
        return bean.size();
    }

    public TableBean getBean() {
        return bean;
    }

    public void setBean(TableBean bean) {
        this.bean = bean;
        for (ColumnBean c : this.bean.getColumns()) {
            this.columns.add(new ColumnModel(this, c));
        }
    }

    public boolean addArrowModel(ArrowModel arrowModel){
        return arrows.add(arrowModel);
    }

    public Set<ColumnModel> getColumns() {
        return columns;
    }

    public void revalidateArrows(){
        for (ArrowModel v : arrows) {
            v.revalidate();
        }
    }

    @Override
    public void repaint(){
        super.repaint();
        for (ArrowModel v : arrows) {
            v.repaint();
        }
    }

    @Override
    public String toString() {
        return String.valueOf(getName());
    }

    @Override
    public boolean hasChildrens() {
        return !columns.isEmpty();
    }

    @Override
    public Collection getChildrens() {
        return columns;
    }

    @Override
    public boolean isSelected() {
        for (ColumnModel c : columns) {
            if (c.isSelected()){
                return true;
            }
        }

        return super.isSelected();
    }

    @Override
    public void setSelected(boolean selected) {
        for (ColumnModel c : columns) {
            c.setSelected(selected);
        }

        super.setSelected(selected);
    }


}
